﻿using MyFileView.Core;
using MyFileView.Core.Constant;
using MyFileView.Core.Core;
using MyFileView.Core.DocumentFormat;
using MyFileView.Core.Service;
using MyFileView.Core.Service.Pdf;
using MyFileView.Shard.Util;

using System;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Threading.Tasks;

using unoidl.com.sun.star.awt;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.sheet;

namespace MyFleView.Core.Service
{
    //IDocumentFormatRegistry documentFormatRegistry,
    public class OfficeFilePreview(IConvertCore convertCore, IDocumentFormatRegistry documentFormatRegistry,IPdfConvertServie pdfConvertServie, IConvertFileFactory convertFileFactory)
    : AbstractOfficeDocumentConvert(convertFileFactory)//(documentFormatRegistry)
    {

       
        //override
        //"xls", "xlsx",  适合前端
        public override string[] Name { get; } = new string[] { "docx", "doc", "ppt", "xls", "xlsx","ppt","pptx","xlsm","ods" };//, "xls", "xlsx"
        /// <summary>
        ///转换类型
        /// </summary>
        public string[] ConvertType { get; } = new string[] { "docx", "doc", "ppt", "pptx", "ods" };

        /// <summary>
        ///不转换类型
        /// </summary>
        public string[] UnconvertType { get; } = new string[] { "xls", "xlsx","xlsm" };

        /// <summary>
        /// 文件预览
        /// </summary>
        /// <returns></returns>
        public override  void FilePreview()
        { 
            ///xls文件
            if (UnconvertType.Contains(ConvertFileContext.FileInfo.FileSuffix))
            {
                ConvertFileContext.FileInfo.OfficeOutPutFullFilePath = FileConstant.Xlsx;
                SetCollection(ConvertFileContext.FileInfo.FullFilePath, RouteConstant.Excel);
               // return RouteConstant.Excel;
            }
            
            if (ConvertType.Contains(ConvertFileContext.FileInfo.FileSuffix))
            {
                 ConvertFileContext.FileInfo.OfficeOutPutFullFilePath = FileConstant.Pdf;
                 ConvertToPdf();
                 pdfConvertServie.ConvertToPng();
                // return RouteConstant.Picture;
            }
            //这里Connection 要先用到
           // return RouteConstant.Index;
        }

        public void ConvertToPdf()
        {    ///生成pdf地址
            string path = ConvertFileContext.FileInfo.OutPutFilePath;
           
            //string fullpath=   Path.GetFullPath();
            if (!FileUtil.IsFileExists(path))
            {   
                ExecutionConvert();        
                SetCollection(path, RouteConstant.Pdf);
            }
        }
        public void SetCollection(string path,string action )
        {
            var afterConvert = ConvertFileContext.AfterConvertPathHandle;
            afterConvert.SetCollection(path);
            afterConvert.Action = action;

        }
        /// <summary>
        /// 后缀数据转化为参数
        /// </summary>
        /// <returns></returns>
        private PropertyValue[] GetFileSuffixProperty()
        {
            var documentData = documentFormatRegistry.GetFormatByExtension(ConvertFileContext.FileInfo.OutPutFileSuffix);
            return GenerateFileConvertProperty(documentData);
        }
   

        public void ExecutionConvert()
        {
            convertCore.CreateComponentContext();
            convertCore.Connection();
            convertCore.SaveDocument( GetFileSuffixProperty());
        }

    }
}